import http from '@/common/http.js'
const uploadImg = data => {
	return new Promise((resolve, reject) => {
		let imgList = []
		uni.showLoading({
			title: '上传中'
		})
		// try {
			let i = 0
			function upload(){
				uni.uploadFile({
					// url: http.BASE_URL + 'Common/upload',
					url: http.BASE_URL + 'upload/uploadFile',
					header: {
						token: uni.getStorageSync('token')
					},
					name: 'file',
					// #ifdef MP
					filePath: data.tempFilePaths[i],
					// #endif
					// #ifdef H5 || APP
					files: [{
						name: 'file',
						file: data.tempFiles[i],
						uri: data.tempFilePaths[i],
					}],
					// #endif
					success: res => {
						let info = JSON.parse(res.data)
						if (info.code == 1) {
							i++
							imgList.push(info.data.fullurl)
							if (data.tempFiles.length == i) {
								uni.hideLoading()
								resolve(imgList)
								return
							}
							upload()
						}
						console.log(i)
						
					},
					fail(err) {
						console.log(err)
					},
					template() {
						console.log('1231311312')
					}
				});
			}
			upload()
			
			// data.tempFilePaths.forEach((item, i) => {
			// 	uni.uploadFile({
			// 		// url: http.BASE_URL + 'Common/upload',
			// 		url: http.BASE_URL + 'upload/uploadFile',
			// 		header: {
			// 			token: uni.getStorageSync('token')
			// 		},
			// 		name: 'file',
			// 		// #ifdef MP
			// 		filePath: data.tempFilePaths[i],
			// 		// #endif
			// 		// #ifdef H5 || APP
			// 		files: [{
			// 			name: 'file',
			// 			file: data.tempFiles[i],
			// 			uri: data.tempFilePaths[i],
			// 		}],
			// 		// #endif
			// 		success: res => {
			// 			let info = JSON.parse(res.data)
			// 			if (info.code == 1) {
			// 				imgList.push(info.data.fullurl)
			// 			}
			// 			console.log(i)
			// 			if (data.tempFiles.length == i + 1) {
			// 				uni.hideLoading()
			// 				resolve(imgList)
			// 			}
			// 		},
			// 		fail(err) {
			// 			console.log(err)
			// 		},
			// 		template() {
			// 			console.log('1231311312')
			// 		}
			// 	});
			// })
		// } catch (e) {
		// 	uni.hideLoading()
		// 	uni.showToast({
		// 		title: '服务器或网络异常，请稍候重试',
		// 		icon: 'none'
		// 	})
		// }
	})

}

const uploadVideo = (data) => {
	console.log(data)
	console.log(data.tempFilePath)
	return new Promise((resolve, reject) => {
		try {
			uni.uploadFile({
				url: http.BASE_URL + 'upload/uploadFile',
				header: {
					token: uni.getStorageSync('token')
				},
				name: 'file',
				filePath: data.tempFilePath,
				success: res => {
					console.log(res)
					let info = JSON.parse(res.data)
					if (info.code == 1) {
						console.log(info)
						resolve(info.data.fullurl)
					}
				},
				fail(err) {
					console.log(err)
				},
				template() {
					console.log('1231311312')
				}
			});
		} catch (e) {
			uni.hideLoading()
			uni.showToast({
				title: '服务器或网络异常，请稍候重试',
				icon: 'none'
			})
		}

	})
}

const uploadAudio = (data) => {
	console.log('------------语音')
	console.log(data)
	uni.showLoading({
		title: '上传中'
	})
	return new Promise((resolve, reject) => {
		try {
			uni.uploadFile({
				url: http.BASE_URL + 'upload/uploadFile',
				header: {
					token: uni.getStorageSync('token')
				},
				filePath: data,
				name: 'file',
				success: res => {
					uni.hideLoading()
					let info = JSON.parse(res.data)
					if (info.code == 1) {
						console.log(info)
						resolve(info.data.fullurl)
					} else {
						uni.showToast({
							title: info.msg
						})
					}
				},
				fail(err) {
					console.log(err)
				},
				template() {
					uni.hideLoading()
					console.log('1231311312')
				}
			});
		} catch (e) {
			uni.hideLoading()
			uni.showToast({
				title: '服务器或网络异常，请稍候重试',
				icon: 'none'
			})
		}

	})
}

export default {
	uploadImg, // 上传图片
	uploadVideo, // 上传视频
	uploadAudio, // 上传音频
}
